package cn.jellybmedia.org.controller;

import cn.jellybmedia.basic.exception.BusinessException;
import cn.jellybmedia.basic.utils.ExcelUtil;
import cn.jellybmedia.org.domain.ShopAuditLog;
import cn.jellybmedia.org.service.IShopService;
import cn.jellybmedia.org.domain.Shop;
import cn.jellybmedia.org.query.ShopQuery;
import cn.jellybmedia.basic.utils.AjaxResult;
import cn.jellybmedia.basic.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController{
    @Autowired
    public IShopService shopService;

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public QueryResult<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryData(query);
    }



    /**======================================================================================
    * @Title: addOrUpt
    * @Description: 商家入住的接口
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/1 15:04
    * @Parameters: [shop]
    * @Return cn.jellybmedia.basic.utils.AjaxResult
    ======================================================================================*/
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();

            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            // 执行失败,进行返回,并返回提示信息
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        }

    }

    /**======================================================================================
    * @Title: reject
    * @Description: 店铺驳回接口
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/2 11:47
    * @Parameters: [shop]
    * @Return cn.jellybmedia.basic.utils.AjaxResult
    ======================================================================================*/
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙,请稍后重试");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**======================================================================================
    * @Title: pass
    * @Description: 店铺审核通过接口
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/2 16:56
    * @Parameters: [shopAuditLog]
    * @Return cn.jellybmedia.basic.utils.AjaxResult
    ======================================================================================*/
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**======================================================================================
    * @Title: exportExcel
    * @Description: 报表导出
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/2 16:55
    * @Parameters: [response]
    * @Return void
    ======================================================================================*/
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response) {
        List<Shop> shops = shopService.loadAll();
        // 第一个是遍历到的集合,第二个是title,第三个是店铺的列表名,第四个是字节码文件,第五个是文件名
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xlsx",response);
    }

    /**======================================================================================
    * @Title: importExcel
    * @Description: 导入报表
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/2 16:55
    * @Parameters: [file]
    * @Return void
    ======================================================================================*/
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file")MultipartFile file) {
        // 第一个:接收的文件.第二个:标题列,第三个是表的主体,第四个是生成的字节码.
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);

        shopService.addList(shops);

        // 打印看看效果
        System.out.println(shops);
    }

    /**======================================================================================
    * @Title: loadShopEcharts
    * @Description: 首页树状图
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/3 11:14
    * @Parameters: []
    * @Return java.util.Map<java.lang.String,java.lang.Object>
    ======================================================================================*/
    @GetMapping("/echarts")
    public Map<String,Object> loadShopEcharts(){

        return shopService.loadShopEcharts();
    }

    /**======================================================================================
     * @Title: refuse
     * @Description: TODO
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/7/3 11:15
     * @Parameters: [shopAuditLog]
     * @Return cn.jellybmedia.basic.utils.AjaxResult
    ======================================================================================*/
    @PostMapping("/refuse")
    public AjaxResult refuse(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            System.out.println("controller层"+shopAuditLog);
            shopService.refuse(shopAuditLog);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }



    /**======================================================================================
    * @Title: getTransform
    * @Description: 重新注册数据回显
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/3 15:49
    * @Parameters: [id]
    * @Return cn.jellybmedia.org.domain.Shop
    ======================================================================================*/
    @GetMapping("/transform/{id}")
    public Shop getTransform(@PathVariable("id")Long id)
    {
        return shopService.getTransform(id);
    }
}
